import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
// import { IHandle } from '@dfeidao/core/on';

import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import render from '@dfeidao/atom-web/render/render';
// import render_tpl from '@dfeidao/atom-web/render/data-render';
// import { fromString, place, Position, remove } from '@dfeidao/core/dom';
import a015 from './a015';
import P02 from './P02';

interface Aa {
	param: HTMLElement;
}
interface Bb {
	param: HTMLElement;
}
interface Cc {
	field: string;
	field_name: string;
	field_value: string;
}


export default async function a017(fd: IFeidaoAiBrowserComponent, args: { currentTarget: HTMLElement }) {

	const field = args.currentTarget.dataset.feidaoId;
	const aaa = get(fd, 'name2title') as Array<{ w: string, n: string }>;

	// console.log(aaa)
	const name2title = {};
	aaa.map((a) => {
		name2title[a.w] = a.n;
	});
	// console.log(name2title);
	//
	//  确定按钮(text)

	const q = {};
	const input_node = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id=input_' + field + ']');
	const v = input_node.value;
	if (v) {
		q[field] = v;
	} else {
		q[field] = '';
	}
	input_node.value = '';

	const condition_node = get(fd, 'condition_node') as Aa;
	const condition_evt = get(fd, 'condition_evt') as Bb;
	const criteria = get(fd, 'criteria');
	// console.log('--------a017---------');
	// console.log(condition_node)
	// 渲染条件
	if (condition_node.param[field]) {
		delete condition_node.param[field];
		(condition_evt.param[field][0]).destroy();
		delete condition_evt.param[field];
		const r_node = fd.data.node.querySelector('#condition_' + field);
		r_node.parentNode.removeChild(r_node);
	}
	const tpl_d = {} as Cc;
	tpl_d.field = field;
	tpl_d.field_name = name2title[field];
	tpl_d.field_value = q[field] || '空';


	// const tpl_str = render_tpl(tpl_d, P02);

	// const node = fd.data.node.querySelector('[data-feidao-presentation=P02]');
	// place(fromString(tpl_str), Position.LastIn, node);
	render(fd, tpl_d, P02, 'P02', 'lastin');

	condition_node.param[field] = fd.data.node.querySelector('[data-feidao-id=' + field + ']');
	// condition_evt.param[field] = fd.parse_event(condition_node.param[field]);

	// 刷新列表
	criteria[field] = (() => {
		if (q[field] === '') {
			return '';
		} else {
			return { $like: `%${q[field]}%` };
		}
	})();
	set(fd, 'page_no', 1);
	set(fd, 'criteria', criteria);
	set(fd, 'condition_node', condition_node);
	set(fd, 'condition_evt', condition_evt);
	await a015(fd);
}
